﻿<?php


$obj=new mysql($config);

//查找出商品的分类
$sql="select * from `cate` where `cate_type`=2";  //cate_type等于2代表的是分类产品类
$date=$obj->getCondition($sql);
$cates=getc($date,0);


//查找出商品的属性
$sql="select * from `attr`";  //cate_type等于2代表的是分类产品类
$attr=$obj->getCondition($sql);
// print_r($attr);




if(!empty($_POST)){
            $arr=array();
             //展示图片上传
            if(!empty($_FILES['orignalimg']['name'])){
                $orignalimg=upload($_FILES['orignalimg']);
                $thumbimg=thumb($orignalimg);
            }

            $arr['orignalimg']=$orignalimg;
            $arr['thumbimg']=$thumbimg;
            $arr['goods_name']=$_POST['goods_name'];
            $arr['price']=$_POST['price'];
            $arr['cid']=$_POST['cid'];
            $arr['des']=$_POST['des'];
            $arr['createtime']=time();

            $row=$obj->insert($arr,'goods');
            $id=$obj->getInsertid();


           //  //获得刚刚插入的商品的id号，将id连同下面的其它图片插入到goosimg表中
           $other=checkimg($_FILES['orignal_img']);
           if(!empty($other['name'][0])){
                       $arrlist=array();
                       foreach ($other['name'] as $k => $v) {
                           $arrlist[$k]['name']=$other['name'][$k];
                           $arrlist[$k]['type']=$other['type'][$k];
                           $arrlist[$k]['size']=$other['size'][$k];
                           $arrlist[$k]['tmp_name']=$other['tmp_name'][$k];
                           $arrlist[$k]['error']=$other['error'][$k];
                       }

                       $goodsimg=array();
                       $goodsimg['goods_id']=$id;
                       foreach ($arrlist as $k => $v) {
                            $orignal_img=upload($v);
                            $thumb_img=thumb($orignal_img);
                            $goodsimg['orignal_img']=$orignal_img;
                            $goodsimg['thumb_img']=$thumb_img;
                            $obj->insert($goodsimg,'goodsimg');
                       }

           }



           // print_r($_POST);die;

    //在成功插入商品之后才可以将对应的属性插入到属性表中
    if($row){
        $i=0;
        foreach ($_POST['attr_id'] as $k => $v) {
                 if(!is_array($v)){
                        if(!empty($v)){
                            $obj->insert(array('goods_id'=>$id,'attr_id'=>$k,'attr_value'=>$v),'goodsattr');
                         }

                 }else{

                        foreach ($v as $k1 => $v1) {
                            $obj->insert(array('goods_id'=>$id,'attr_id'=>$k,'attr_value'=>$v1,'price'=>$_POST['prices'][$i]),'goodsattr');
                            $i++;
                        }
                 }
        }




        echo '<script>alert("添加成功");</script>';
    }else{
        echo '<script>alert("添加失败");</script>';
    }
}









require_once (VIEW_PATH.'admin/layout.html');

